import { Injectable } from '@angular/core';
import { ApolloQueryResult, FetchResult } from '@apollo/client/core';
import { Apollo, gql, QueryRef } from 'apollo-angular';
import { Observable } from 'rxjs';
import { TenantServiceApi,
TenantService_getTenantResult,
TenantService_getTenantVariables,
TenantService_listResult,
TenantService_listVariables,
TenantService_totalCountResult,
TenantService_searchResult,
TenantService_searchVariables,
TenantService_loadAllResult,
TenantService_getByIdResult,
TenantService_getByIdVariables,
TenantService_createTenantResult,
TenantService_createTenantVariables,
TenantService_saveResult,
TenantService_saveVariables,
TenantService_removeByIdResult,
TenantService_removeByIdVariables,
TenantService_removeByIdListResult,
TenantService_removeByIdListVariables,
TenantResultInfo, TenantPageInfo,
TenantConnection,
TenantPackage,
TenantOrderBy,
TenantMutationInfo,
TenantStringFilter,
TenantNumberFilter,
TenantBooleanFilter,
tenantServiceQuery,
tenantServiceMutation,
TenantServiceQueryOptions,
TenantStore,
TenantView,
TenantInput,
TenantFilter,
TenantErrorBody} from '../model/tenant_service.model';

/************************************************************************
 * 学校管理
 */
@Injectable({
    providedIn: 'root'
})
export class TenantService implements TenantServiceApi {

        private getTenantGql:string;
        private listGql:string;
        private totalCountGql:string;
        private searchGql:string;
        private searchObjectRef: QueryRef<TenantService_searchResult>;
        private loadAllGql:string;
        private getByIdGql:string;

    constructor(private apollo: Apollo) {
        this.getTenantGql=`query aap_tenantService_getTenant($code: String!){${tenantServiceQuery.tenantService_getTenant}}`;
        this.listGql=`query aap_tenantService_list($filter: TenantFilter!,$orderBy: OrderBy){${tenantServiceQuery.tenantService_list}}`;
        this.totalCountGql=`query aap_tenantService_totalCount{${tenantServiceQuery.tenantService_totalCount}}`;
        this.searchGql=`query aap_tenantService_search($first: Int!,$offset: Float!,$filter: TenantFilter!,$orderBy: OrderBy){${tenantServiceQuery.tenantService_search}}`;
        this.searchObjectRef = apollo.watchQuery<TenantService_searchResult>({ query: gql`${this.searchGql}`, fetchPolicy:'cache-first' });
        this.loadAllGql=`query aap_tenantService_loadAll{${tenantServiceQuery.tenantService_loadAll}}`;
        this.getByIdGql=`query aap_tenantService_getById($id: String!){${tenantServiceQuery.tenantService_getById}}`;
    }
    /**
     * 获取学校信息
     */
    getTenant(variables: TenantService_getTenantVariables, options:TenantServiceQueryOptions = { cachePolicy: 'no-cache', more: false }): Observable<ApolloQueryResult<TenantService_getTenantResult>> {
            return this.apollo.query({ query: gql`${this.getTenantGql}`,variables ,fetchPolicy:options.cachePolicy});
    }
    /**
     * 分页查询
     */
    list(variables: TenantService_listVariables, options:TenantServiceQueryOptions = { cachePolicy: 'no-cache', more: false }): Observable<ApolloQueryResult<TenantService_listResult>> {
            return this.apollo.query({ query: gql`${this.listGql}`,variables ,fetchPolicy:options.cachePolicy});
    }
    /**
     * 统计库中所有对象的数据量
     */
    totalCount(options:TenantServiceQueryOptions = { cachePolicy: 'no-cache', more: false }): Observable<ApolloQueryResult<TenantService_totalCountResult>> {
            return this.apollo.query({ query: gql`${this.totalCountGql}`,fetchPolicy:options.cachePolicy});
    }
    /**
     * 游标查询
     */
    search(variables: TenantService_searchVariables, options:TenantServiceQueryOptions = { cachePolicy: 'cache-first', more: false }): Observable<ApolloQueryResult<TenantService_searchResult>> {
        this.searchObjectRef.setVariables(variables);
        if(options.more){
            this.searchObjectRef.fetchMore({variables});
        }
        return this.searchObjectRef.valueChanges;
    }
    /**
     * 获取库中所有数据，数据量大时请谨慎使用
     */
    loadAll(options:TenantServiceQueryOptions = { cachePolicy: 'cache-first', more: false }): Observable<ApolloQueryResult<TenantService_loadAllResult>> {
            return this.apollo.query({ query: gql`${this.loadAllGql}`,fetchPolicy:options.cachePolicy});
    }
    /**
     * 获取一个对象
     */
    getById(variables: TenantService_getByIdVariables, options:TenantServiceQueryOptions = { cachePolicy: 'cache-first', more: false }): Observable<ApolloQueryResult<TenantService_getByIdResult>> {
            return this.apollo.query({ query: gql`${this.getByIdGql}`,variables ,fetchPolicy:options.cachePolicy});
    }
    /**
     * 创建学校
     */
    createTenant(variables: TenantService_createTenantVariables): Observable<FetchResult<TenantService_createTenantResult>>{
        return this.apollo.mutate({mutation: gql`mutation aap_tenantService_createTenant($tenant: TenantInput!,$password: String!){${tenantServiceMutation.tenantService_createTenant}}`, variables,fetchPolicy:'no-cache'});
    }
    /**
     * 保存对象
     */
    save(variables: TenantService_saveVariables): Observable<FetchResult<TenantService_saveResult>>{
        return this.apollo.mutate({mutation: gql`mutation aap_tenantService_save($entity: TenantInput!){${tenantServiceMutation.tenantService_save}}`, variables,fetchPolicy:'no-cache'});
    }
    /**
     * 删除一个对象
     */
    removeById(variables: TenantService_removeByIdVariables): Observable<FetchResult<TenantService_removeByIdResult>>{
        return this.apollo.mutate({mutation: gql`mutation aap_tenantService_removeById($id: String!){${tenantServiceMutation.tenantService_removeById}}`, variables,fetchPolicy:'no-cache'});
    }
    /**
     * 删除一批对象
     */
    removeByIdList(variables: TenantService_removeByIdListVariables): Observable<FetchResult<TenantService_removeByIdListResult>>{
        return this.apollo.mutate({mutation: gql`mutation aap_tenantService_removeByIdList($ids: [String]!){${tenantServiceMutation.tenantService_removeByIdList}}`, variables,fetchPolicy:'no-cache'});
    }









}
